package com.irdstudio.efp.riskm.api.rest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.console.service.facade.PubSysInfoService;
import com.irdstudio.efp.cus.service.vo.CusIndivVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.loan.service.facade.AccLoanService;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.efp.riskm.service.facade.RscAdjustAppService;
import com.irdstudio.efp.riskm.service.facade.RscAdjustBillService;
import com.irdstudio.efp.riskm.service.vo.RscAdjustAppVO;
import com.irdstudio.efp.riskm.service.vo.RscAdjustBillVO;

@RestController
@RequestMapping("/api")
public class RscAdjustBillController extends AbstractController implements FrameworkService {

//	@Autowired
//	@Qualifier("rscAdjustBillService")
//	private RscAdjustBillService rscAdjustBillService;
//	@Autowired
//	@Qualifier("accLoanService") // DEL
//	private AccLoanService accLoanService;
//	// @Autowired
//	// @Qualifier("cusIndivService")
//	// private CusIndivService cusIndivService;
//	@Autowired
//	@Qualifier("rscAdjustAppService")
//	private RscAdjustAppService rscAdjustAppService;
//	@Autowired
//	@Qualifier("pubSysInfoService")
//	private PubSysInfoService pubSysInfoService;
//	@Autowired
//	@Qualifier("sRoleDataRuleService")
//	private SRoleDataRuleService sRoleDataRuleService;
//
//	/**
//	 * 列表数据查询
//	 * 
//	 * @param page
//	 * @param size
//	 * @return
//	 */
//	@RequestMapping(value = "/rsc/adjust/bills", method = RequestMethod.POST)
//	public @ResponseBody ResponseData<List<RscAdjustBillVO>> queryRscAdjustBillAll(@RequestBody RscAdjustBillVO vo) {
//		// List<RscAdjustBillVO> outputVo = this.executeQueryList("queryAll", rscAdjustBillService, new Object[]{vo});
//		List<RscAdjustBillVO> outputVo = rscAdjustBillService.queryAllOwner(vo);
//		return getResponseData(outputVo);
//
//	}
//
//	/**
//	 * 条件查询
//	 * 
//	 * @param page
//	 * @param size
//	 * @return
//	 */
//	@RequestMapping(value = "/rsc/adjust/bills/remove", method = RequestMethod.POST)
//	public @ResponseBody ResponseData<List<RscAdjustBillVO>> queryRscAdjustBillTwo(@RequestBody RscAdjustBillVO vo) {
//		setUserInfoToVO(vo);
//		setUserInfoToVO(vo);
//		// 根据用户角色获取用户权限方案
//		String dataAuthority = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
//		List<RscAdjustBillVO> outputVo = null;
//		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataAuthority);
//
//		// 根据权限方案调用不同的方案进行数据查询
//		switch (dataRuleType) {
//		case Owner:
//			outputVo = rscAdjustBillService.queryAllOwner(vo);
//			break;
//		case CurrOrg:
//			outputVo = rscAdjustBillService.queryAllCurrOrg(vo);
//			break;
//		case CurrDownOrg:
//			outputVo = rscAdjustBillService.queryAllCurrDownOrg(vo);
//			break;
//		default:
//			outputVo = rscAdjustBillService.queryAllOwner(vo);
//			break;
//		}
//		return getResponseData(outputVo);
//
//	}
//
//	/**
//	 * 根据主键查询详情
//	 * 
//	 * @return
//	 */
//	@RequestMapping(value = "/rsc/adjust/bill/", method = RequestMethod.GET)
//	public @ResponseBody ResponseData<RscAdjustBillVO> queryByPk() {
//		RscAdjustBillVO inVo = new RscAdjustBillVO();
//		RscAdjustBillVO outputVo = rscAdjustBillService.queryByPk(inVo);
//		return getResponseData(outputVo);
//
//	}
//
//	/**
//	 * 根据主键删除信息
//	 * 
//	 * @param rscAdjustBill
//	 * @return
//	 */
//	@RequestMapping(value = "/rsc/adjust/bill", method = RequestMethod.DELETE)
//	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody RscAdjustBillVO inRscAdjustBillVo) {
//		int outputVo = rscAdjustBillService.deleteByPk(inRscAdjustBillVo);
//		return getResponseData(outputVo);
//
//	}
//
//	/**
//	 * 根据主键更新信息
//	 * 
//	 * @param inRscAdjustBillVo
//	 * @return
//	 */
//	@RequestMapping(value = "/rsc/adjust/bill", method = RequestMethod.PUT)
//	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody RscAdjustBillVO inRscAdjustBillVo) {
//		int num = 0;
//		RscAdjustBillVO rscAdjustBillVO = new RscAdjustBillVO();
//		beanCopy(inRscAdjustBillVo, rscAdjustBillVO);
//		num = rscAdjustBillService.updateByTaskNo(rscAdjustBillVO);
//		return getResponseData(num);
//	}
//
//	/**
//	 * 新增数据
//	 * 
//	 * @param inRscAdjustBillVo
//	 * @return
//	 */
//	@RequestMapping(value = "/rsc/adjust/bill", method = RequestMethod.POST)
//	public @ResponseBody ResponseData<Integer> insertRscAdjustBill(@RequestBody RscAdjustBillVO inRscAdjustBillVo) {
//		int num = 0;
//		int num1 = 0;
//		int num2 = 0;
//		int num3 = 0;
//		int OverdueDays = 0;
//		AccLoanVO accLoanVO = new AccLoanVO();
//		RscAdjustAppVO rscAdjustApp = new RscAdjustAppVO();
//
//		beanCopy(inRscAdjustBillVo, rscAdjustApp);
//		String billNo = inRscAdjustBillVo.getBillNo();
//		List<String> billNoList = null;
//		// 判断billNo是否为空
//		if (billNo != null && !"".equals(billNo)) {
//			String[] billno = billNo.split(",");
//			billNoList = Arrays.asList(billno);
//		}
//		logger.info("billNoList参数：" + JSONObject.toJSONString(billNoList));
//		List<RscAdjustBillVO> lists = new ArrayList<RscAdjustBillVO>(billNoList.size());
//		RscAdjustBillVO rscAdjustBillVO;
//		for (int b = 0; b < billNoList.size(); b++) {
//			rscAdjustBillVO = new RscAdjustBillVO();
//			rscAdjustBillVO.setBillNo(billNoList.get(b));
//			logger.info("rscAdjustBillVO参数：" + JSONObject.toJSONString(rscAdjustBillVO));
//			lists.add(rscAdjustBillVO);
//		}
//		List<RscAdjustBillVO> rscAdjustBillList = new ArrayList<RscAdjustBillVO>();
//		for (int n = 0; n < lists.size(); n++) {
//			RscAdjustBillVO RscAdjustBillVO = lists.get(n);
//			beanCopy(RscAdjustBillVO, accLoanVO);
//			logger.info("accLoanVO参数：" + JSONObject.toJSONString(accLoanVO));
//			AccLoanVO AccLoan = accLoanService.queryByPk(accLoanVO);
//			logger.info("AccLoan参数：" + JSONObject.toJSONString(AccLoan));
//			RscAdjustBillVO rscAdjustBill = new RscAdjustBillVO();
//			beanCopy(AccLoan, rscAdjustBill);
//			rscAdjustBill.setBillNo(AccLoan.getBillNo());
//			rscAdjustBill.setTaskNo(inRscAdjustBillVo.getTaskNo());
//			String capOverdueDate = AccLoan.getCapOverdueDate();
//			String intOverdueDate = AccLoan.getIntOverdueDate();
//			logger.info("本金逾期起始日为：" + capOverdueDate + "，利息逾期起始日为：" + intOverdueDate);
//			try {
//				if ((capOverdueDate != null && !"".equals(capOverdueDate)) || (intOverdueDate != null && !"".equals(intOverdueDate))) {
//					String openDay = pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001);
//					logger.info("当前营业日期为：" + openDay);
//					if ((capOverdueDate != null && !"".equals(capOverdueDate)) && (intOverdueDate != null && !"".equals(intOverdueDate))) {
//						num1 = TimeUtil.getDaysBetweenTwoDates(capOverdueDate, openDay);
//						num2 = TimeUtil.getDaysBetweenTwoDates(intOverdueDate, openDay);
//						logger.info("本金逾期起始日与当前营业日期相差：" + num1 + "利息逾期起始日与当前营业日期相差：" + num2);
//						if (num1 > num2) {
//							OverdueDays = num1;
//						} else if (num1 < num2) {
//							OverdueDays = num2;
//						} else if (num1 == num2) {
//							OverdueDays = num1;
//						}
//						logger.info("逾期天数：" + OverdueDays);
//						rscAdjustBill.setCapOverdueDays(BigDecimal.valueOf(num1));
//						rscAdjustBill.setIntOverdueDays(new BigDecimal(num2));
//						rscAdjustBill.setOverdueDays(new BigDecimal(OverdueDays));
//					} else if ((capOverdueDate == null || "".equals(capOverdueDate))
//							&& (intOverdueDate != null && !"".equals(intOverdueDate))) {
//						num2 = TimeUtil.getDaysBetweenTwoDates(intOverdueDate, openDay);
//						logger.info("利息逾期起始日不为空，与当前营业日期相差：" + num2);
//						rscAdjustBill.setIntOverdueDays(new BigDecimal(num2));
//						rscAdjustBill.setCapOverdueDays(new BigDecimal(0));
//						logger.info("逾期天数：" + num2);
//						rscAdjustBill.setOverdueDays(new BigDecimal(num2));
//					} else if ((capOverdueDate != null && !"".equals(capOverdueDate))
//							&& (intOverdueDate == null || "".equals(intOverdueDate))) {
//						num1 = TimeUtil.getDaysBetweenTwoDates(capOverdueDate, openDay);
//						logger.info("本金逾期起始日不为空，与当前营业日期相差：" + num1);
//						rscAdjustBill.setCapOverdueDays(new BigDecimal(num1));
//						rscAdjustBill.setIntOverdueDays(new BigDecimal(0));
//						logger.info("逾期天数：" + num1);
//						rscAdjustBill.setOverdueDays(new BigDecimal(num1));
//					}
//				} else {
//					logger.info("本金逾期起始日和利息逾期起始日为空,设值为0");
//					rscAdjustBill.setIntOverdueDays(new BigDecimal(0));
//					rscAdjustBill.setCapOverdueDays(new BigDecimal(0));
//					rscAdjustBill.setOverdueDays(new BigDecimal(0));
//				}
//			} catch (Exception e) {
//				logger.error("新增借据数据出错!", e);
//			}
//			CusIndivVO cusIndivVo = new CusIndivVO();
//			cusIndivVo.setCusId(AccLoan.getCusId());
//			CusIndivVO cusIndiv = new CusIndivVO();// cusIndivService.queryByPk(cusIndivVo);
//			if (Objects.nonNull(cusIndiv) && cusIndiv != null) {
//				logger.info("客户信息为：" + JSONObject.toJSONString(cusIndiv));
//				String crdGrade = cusIndiv.getCrdGrade();
//				if (crdGrade != null && !"".equals(crdGrade)) {
//					rscAdjustBill.setCreditGrade(crdGrade);
//					rscAdjustBill.setGuarStatus("无异常");
//				} else {
//					rscAdjustBill.setGuarStatus("无异常");
//				}
//			}
//			rscAdjustBill.setAutoClaResult(AccLoan.getAutoResult());
//			rscAdjustBill.setPreAdjustResult(AccLoan.getSevenResult());
//			rscAdjustBill.setCusManager(AccLoan.getCusManager());
//			rscAdjustBill.setClaDate("");
//			logger.info("rscAdjustBill参数为：" + JSONObject.toJSONString(rscAdjustBill));
//			RscAdjustBillVO outputVo = rscAdjustBillService.queryByPk(rscAdjustBill);
//			logger.info("outputVo参数为：" + JSONObject.toJSONString(outputVo));
//			if (Objects.nonNull(outputVo)) {
//				num = rscAdjustBillService.deleteByPk(rscAdjustBill);
//			}
//			rscAdjustBillList.add(rscAdjustBill);
//		}
//		if (rscAdjustBillList != null && rscAdjustBillList.size() > 0) {
//			logger.info("批量插入条数：" + rscAdjustBillList.size());
//			logger.info("批量插入参数为：" + JSONObject.toJSONString(rscAdjustBillList));
//			num = rscAdjustBillService.insertRscAdjustBill(rscAdjustBillList);
//		}
//		logger.info("更新主表参数为：" + JSONObject.toJSONString(rscAdjustApp));
//		num3 = rscAdjustAppService.updateByPk(rscAdjustApp);
//		if (num3 != 1) {
//			logger.error("修改主表出错！");
//		}
//		return getResponseData(num);
//
//	}
}
